package com.example.innovate.service.impl;

import com.example.innovate.domain.EnterpriseDO;
import com.example.innovate.domain.EnterpriseDOExample;
import com.example.innovate.mapper.EnterpriseDOMapper;
import com.example.innovate.service.EnterpriseService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    EnterpriseDOMapper enterpriseDOMapper;

    @Override
    public EnterpriseDO addInfo(EnterpriseDO enterpriseDO) {
        enterpriseDOMapper.insertSelective(enterpriseDO);
        return enterpriseDO;
    }

    @Override
    public EnterpriseDO findById(int id) {
        return enterpriseDOMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteInfo(int id) {
        enterpriseDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public EnterpriseDO updateInfo(EnterpriseDO enterpriseDO) {
        EnterpriseDOExample enterpriseDOExample = new EnterpriseDOExample();
        enterpriseDOExample.createCriteria().andEnIdEqualTo(enterpriseDO.getEnId());
        enterpriseDOMapper.updateByExampleSelective(enterpriseDO,enterpriseDOExample);
//        enterpriseDOMapper.updateByPrimaryKeySelective(enterpriseDO);
        return enterpriseDO;
    }

    @Override
    public List<EnterpriseDO> findAll(String name, int pageNumber, int pageSize) {
        EnterpriseDOExample enterpriseDOExample = new EnterpriseDOExample();
        if (!StringUtils.isEmpty(name)) {
            enterpriseDOExample.createCriteria().andEnNameEqualTo(name);
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<EnterpriseDO> list = enterpriseDOMapper.selectByExample(enterpriseDOExample);
        return list;
    }

    @Override
    public EnterpriseDO findByPicture(String name) {
        EnterpriseDOExample enterpriseDOExample = new EnterpriseDOExample();
        enterpriseDOExample.createCriteria().andEnQrCodeEqualTo(name);
        List<EnterpriseDO> list = enterpriseDOMapper.selectByExample(enterpriseDOExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
